print("202501141400 - DataService所有数据打印器")

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

-- 使用你成功的日志函数
local function log(s)
    local success, err = pcall(function()
        appendfile("dataservice_all_log.txt", string.format("[%s] %s\n", os.date("%Y-%m-%d %H:%M:%S"), tostring(s)))
    end)
    if not success then
        writefile("dataservice_all_log.txt", string.format("[%s] %s\n", os.date("%Y-%m-%d %H:%M:%S"), tostring(s)))
    end
end

-- 获取DataService（基于你的成功代码）
local function getDataService()
    local success, dataService = pcall(function()
        return require(ReplicatedStorage.Modules.DataService):GetData()
    end)
    
    if success and dataService then
        log("✅ 成功获取DataService")
        return dataService
    else
        log("❌ 获取DataService失败")
        return nil
    end
end

-- 递归打印表数据
local function printTableData(table, path, depth)
    depth = depth or 0
    if depth > 15 then return end -- 防止无限递归
    
    local indent = string.rep("  ", depth)
    
    if typeof(table) == "table" then
        for key, value in pairs(table) do
            local currentPath = path .. "." .. tostring(key)
            
            if typeof(value) == "table" then
                log(indent .. "📁 " .. key .. " (table)")
                printTableData(value, currentPath, depth + 1)
            else
                log(indent .. "📝 " .. key .. " = " .. tostring(value) .. " (" .. typeof(value) .. ")")
            end
        end
    else
        log(indent .. "📝 " .. path .. " = " .. tostring(table) .. " (" .. typeof(table) .. ")")
    end
end

-- 打印DataService的所有数据
local function printAllDataServiceData()
    log("🚀 开始打印DataService的所有数据...")
    
    local dataService = getDataService()
    if not dataService then
        return false
    end
    
    log("📋 DataService顶级字段:")
    local fieldCount = 0
    
    for key, value in pairs(dataService) do
        fieldCount = fieldCount + 1
        log("🔍 字段 " .. fieldCount .. ": " .. key .. " (" .. typeof(value) .. ")")
        
        -- 打印每个字段的详细数据
        log("📋 字段 " .. key .. " 的详细数据:")
        printTableData(value, "DataService." .. key, 1)
        log("") -- 空行分隔
    end
    
    log("📊 打印完成:")
    log("  - 总字段数: " .. fieldCount)
    
    return true
end

-- 导出所有数据为JSON
local function exportAllDataAsJSON()
    log("💾 开始导出DataService所有数据为JSON...")
    
    local dataService = getDataService()
    if not dataService then
        return false
    end
    
    -- 导出JSON
    local okJson, encoded = pcall(function()
        return HttpService:JSONEncode(dataService)
    end)
    
    if okJson then
        pcall(function()
            writefile("dataservice_all_data.json", encoded)
            log("✅ 已导出JSON数据到 dataservice_all_data.json")
        end)
    else
        log("❌ JSON编码失败")
    end
    
    return true
end

-- 创建控制UI
local function createUI()
    local screenGui = Instance.new("ScreenGui")
    screenGui.Name = "DataServiceAllDataPrinter"
    screenGui.ResetOnSpawn = false
    screenGui.Parent = game.CoreGui
    
    local mainFrame = Instance.new("Frame")
    mainFrame.Name = "MainFrame"
    mainFrame.Size = UDim2.new(0, 400, 0, 200)
    mainFrame.Position = UDim2.new(0, 10, 0, 10)
    mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
    mainFrame.BorderSizePixel = 1
    mainFrame.BorderColor3 = Color3.fromRGB(100, 100, 100)
    mainFrame.Parent = screenGui
    
    -- 标题
    local titleLabel = Instance.new("TextLabel")
    titleLabel.Name = "Title"
    titleLabel.Size = UDim2.new(1, 0, 0, 35)
    titleLabel.Text = "DataService 所有数据打印器"
    titleLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
    titleLabel.TextScaled = true
    titleLabel.Font = Enum.Font.SourceSansBold
    titleLabel.BackgroundTransparency = 1
    titleLabel.Parent = mainFrame
    
    -- 打印数据按钮
    local printBtn = Instance.new("TextButton")
    printBtn.Name = "PrintBtn"
    printBtn.Size = UDim2.new(0, 180, 0, 35)
    printBtn.Position = UDim2.new(0, 20, 0, 50)
    printBtn.Text = "打印所有数据 (P)"
    printBtn.TextColor3 = Color3.fromRGB(255, 255, 255)
    printBtn.TextScaled = true
    printBtn.Font = Enum.Font.SourceSans
    printBtn.BackgroundColor3 = Color3.fromRGB(70, 130, 70)
    printBtn.BorderSizePixel = 0
    printBtn.Parent = mainFrame
    
    printBtn.MouseButton1Click:Connect(function()
        printAllDataServiceData()
    end)
    
    -- 导出JSON按钮
    local exportBtn = Instance.new("TextButton")
    exportBtn.Name = "ExportBtn"
    exportBtn.Size = UDim2.new(0, 180, 0, 35)
    exportBtn.Position = UDim2.new(0, 210, 0, 50)
    exportBtn.Text = "导出JSON (E)"
    exportBtn.TextColor3 = Color3.fromRGB(255, 255, 255)
    exportBtn.TextScaled = true
    exportBtn.Font = Enum.Font.SourceSans
    exportBtn.BackgroundColor3 = Color3.fromRGB(130, 70, 70)
    exportBtn.BorderSizePixel = 0
    exportBtn.Parent = mainFrame
    
    exportBtn.MouseButton1Click:Connect(function()
        exportAllDataAsJSON()
    end)
    
    -- 状态显示
    local statusLabel = Instance.new("TextLabel")
    statusLabel.Name = "Status"
    statusLabel.Size = UDim2.new(1, -20, 0, 100)
    statusLabel.Position = UDim2.new(0, 10, 0, 95)
    statusLabel.Text = "按P键打印DataService中的所有数据\n按E键导出所有数据为JSON\n会遍历并打印所有子对象的数据\n不特意找PetsData，显示所有内容"
    statusLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
    statusLabel.TextScaled = true
    statusLabel.Font = Enum.Font.SourceSans
    statusLabel.BackgroundTransparency = 1
    statusLabel.TextWrapped = true
    statusLabel.Parent = mainFrame
    
    -- 关闭按钮
    local closeBtn = Instance.new("TextButton")
    closeBtn.Size = UDim2.new(0, 25, 0, 25)
    closeBtn.Position = UDim2.new(1, -30, 0, 5)
    closeBtn.BackgroundColor3 = Color3.fromRGB(180, 30, 30)
    closeBtn.TextColor3 = Color3.new(1,1,1)
    closeBtn.Font = Enum.Font.GothamBold
    closeBtn.TextScaled = true
    closeBtn.Text = "X"
    closeBtn.Parent = mainFrame
    
    closeBtn.MouseButton1Click:Connect(function()
        screenGui:Destroy()
    end)
    
    return screenGui
end

-- 快捷键控制
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gp)
    if gp then return end
    
    if input.KeyCode == Enum.KeyCode.P then
        printAllDataServiceData()
    elseif input.KeyCode == Enum.KeyCode.E then
        exportAllDataAsJSON()
    end
end)

-- 启动
log("DataService所有数据打印器已启动")
log("按P键打印所有数据，按E键导出JSON")

-- 创建控制UI
createUI()

-- 自动检查
spawn(function()
    wait(3)
    log("🔍 自动检查DataService...")
    local dataService = getDataService()
    if dataService then
        log("✅ 检测到DataService，可以开始打印数据")
        log("📋 DataService顶级字段:")
        local count = 0
        for key, value in pairs(dataService) do
            count = count + 1
            log("  " .. count .. ". " .. key .. " (" .. typeof(value) .. ")")
        end
        log("📊 总共 " .. count .. " 个顶级字段")
    else
        log("ℹ️ 未检测到DataService")
    end
end)
